﻿using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.UI;
using Camc.Quality.Authorization.Users;
using System;
using System.Collections.Generic;
using System.Linq;
using Abp.Extensions;
using Abp.Linq.Extensions;
using System.Text;
using System.Threading.Tasks;
using static Camc.Quality.TimeSetting.CalendarSetting;
using Microsoft.AspNetCore.Mvc;
using Camc.Quality.IOperationHistory;

namespace Camc.Quality.TimeSetting
{
    public class CalendarSettingAppService : QualityAppServiceBase
    {



        private readonly IRepository<CalendarSetting, Guid> _calendarSettingRepository;

        private readonly IOperationHistoryAppService _iOperationHistoryAppService;

        public CalendarSettingAppService(IRepository<CalendarSetting, Guid> calendarSettingRepository,

            IOperationHistoryAppService iOperationHistoryAppService
            )
        {
            _calendarSettingRepository = calendarSettingRepository;
            _iOperationHistoryAppService = iOperationHistoryAppService;
        }
        /// <summary>
        /// 日历设置新增和修改接口，根据是否传输id判断
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task CreateOrUpdateCalendarSetting(CalendarSettingDto input)
        {

            if (input.Id == Guid.Empty)
            {
                await CreateCalendarSetting(input);
            }
            else
            {
                await UpdateCalendarSetting(input);
            }
        }
        /// <summary>
        /// 日历设置表insert
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private async Task<Guid> CreateCalendarSetting(CalendarSettingDto input)
        {
            var CalendarSetting = new CalendarSetting()
            {
                Id = Guid.NewGuid(),
                CalendarName = input.CalendarName,
                StartDate = Convert.ToDateTime(input.StartDate),
                EndDate = Convert.ToDateTime(input.EndDate),
                WorkArrangement = (WorkArrangements)input.WorkArrangement,
            };

            var guid = await _calendarSettingRepository.InsertAndGetIdAsync(CalendarSetting);
            _iOperationHistoryAppService.InsertOperationLog(CalendarSetting.Id, 3, 14, CalendarSetting.CalendarName + "创建成功");
            return guid;
        }
        /// <summary>
        /// 日历设置表update
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private async Task UpdateCalendarSetting(CalendarSettingDto input)
        {
            var calendarSetting = await _calendarSettingRepository.FirstOrDefaultAsync(c => c.Id == input.Id);
            if (calendarSetting != null)
            {
                calendarSetting.CalendarName = input.CalendarName;
                calendarSetting.StartDate = Convert.ToDateTime(input.StartDate);
                calendarSetting.EndDate = Convert.ToDateTime(input.EndDate);
                calendarSetting.WorkArrangement = (WorkArrangements)input.WorkArrangement;
                await _calendarSettingRepository.UpdateAsync(calendarSetting);
            }
            _iOperationHistoryAppService.InsertOperationLog(calendarSetting.Id, 3, 15, calendarSetting.CalendarName + "修改");
        }
        /// <summary>
        /// 删除日历设置
        /// </summary>
        /// <param name = "input" ></param >
        /// <returns ></returns >
        public async Task DeleteCalendarSetting(List<CalendarDeleteDto> input)
        {
            foreach (var item in input)
            {
                var count = await _calendarSettingRepository.CountAsync(p => p.Id == item.Id);
                if (count == 0)
                {
                    throw new UserFriendlyException("");
                }
                else
                {
                    //根据Guid删除CodeFormatDesc数据
                    await _calendarSettingRepository.DeleteAsync(p => p.Id == item.Id);
                }
            }
        }
        /// <summary>
        /// 日历设置查询
        /// </summary>
        /// <param name = "input" ></param >
        /// <returns ></returns >
        public async Task<PagedResultDto<CalendarSettingDto>> GetCalendarSettingList(GetCalendarSettingListInput input)
        {
            var calendarSettingQuery = _calendarSettingRepository.GetAll();
            if (!string.IsNullOrEmpty(input.CalendarName))
            {
                calendarSettingQuery = calendarSettingQuery.Where(d => d.CalendarName == input.CalendarName);
            }
            if (input.WorkArrangement != null)
            {
                calendarSettingQuery = calendarSettingQuery.Where(d => d.WorkArrangement == (WorkArrangements)input.WorkArrangement);
            }
            var count = calendarSettingQuery.Count();
            calendarSettingQuery = calendarSettingQuery.OrderByDescending(e => e.StartDate).PageBy(input);
            var Result = ObjectMapper.Map<List<CalendarSettingDto>>(calendarSettingQuery).ToList();
            return new PagedResultDto<CalendarSettingDto>(count, Result);
        }
        /// <summary>
        /// 获取名称下拉菜单列表
        /// </summary>
        /// <returns></returns>
        public List<TimeSettingDropListDto> GetCalendarNameList()
        {
            //IList<CalendarSettingDto> calendarLists = new List<CalendarSettingDto>();
            var calendarNameList = from calendar in _calendarSettingRepository.GetAll().Select(s => new{s.CalendarName}).Distinct()
                select new TimeSettingDropListDto
                {
                    Label = calendar.CalendarName,
                    Value = calendar.CalendarName
                };
            //foreach (var item in calendarNameList)
            //{
            //    var calendarList = new CalendarSettingDto()
            //    {
            //        CalendarName = item.CalendarName
            //    };
            //    calendarLists.Add(calendarList);
            //}
            //var CalendarSettingDto = ObjectMapper.Map<List<CalendarSettingDto>>(calendarLists);
            if (calendarNameList.Count() > 0)
            {
                return calendarNameList.ToList(); ;
            }
            return new List<TimeSettingDropListDto>();
        }
    }
}
